package com.ecjtu.gateway.train;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ecjtu.gateway.db.po.LeakagePo;
import com.ecjtu.gateway.db.po.NonLeakagePo;
import com.ecjtu.gateway.db.service.LeakageService;
import com.ecjtu.gateway.db.service.NonLeakageService;
import com.ecjtu.gateway.netty.model.DasDataModel;
import com.ecjtu.gateway.netty.model.PacketDataModel;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class TrainSendService {
    private OkHttpClient httpClient = new OkHttpClient();
    private final static String httpHeaderMediaType = "application/json;charset=utf-8";

    private final static String httpHeaderContentType = "Content-Type";
    @Autowired
    private LeakageService leakageService;

    @Autowired
    private NonLeakageService nonLeakageService;

    @PostConstruct
    public void doWork() {
        Long max = 0L;

        List<LeakagePo> dataList = leakageService.queryList(max, 1000);
        while (dataList.size() > 0) {
            for (LeakagePo leakagePo : dataList) {
                doSendToPython(leakagePo);
            }
            Long last = dataList.get(dataList.size() - 1).getId();
            dataList = leakageService.queryList(last, 1000);
        }

        List<NonLeakagePo> nonLeakagePoList = nonLeakageService.queryList(max, 1000);
        while (nonLeakagePoList.size() > 0) {
            for (NonLeakagePo leakagePo : nonLeakagePoList) {
                doSendToPython(leakagePo);
            }
            Long last = nonLeakagePoList.get(nonLeakagePoList.size() - 1).getId();
            nonLeakagePoList = nonLeakageService.queryList(last, 1000);
        }
    }


    private void doSendToPython(LeakagePo leakagePo) {
        JSONObject sendDataJson = new JSONObject();

        sendDataJson.put("d", JSON.parseArray(leakagePo.getData()));
        sendDataJson.put("m", leakagePo.getM());
        sendDataJson.put("time", leakagePo.getTime());
        sendDataJson.put("from", 0);

        //

        RequestBody requestBody = RequestBody.create(MediaType.parse(httpHeaderMediaType), sendDataJson.toJSONString());
        Request request = new Request.Builder()
                .addHeader(httpHeaderContentType, httpHeaderMediaType)
                .addHeader("Accept", httpHeaderMediaType)
                .post(requestBody)
                .url("http://localhost:8899/data1")
                .build();
        Response signRsp = null;
        try {
            signRsp = httpClient.newCall(request).execute();
            if (signRsp.isSuccessful()) {
            }
        } catch (IOException e) {
            log.error("连接服务 失败", e);
        }

    }

    private void doSendToPython(NonLeakagePo leakagePo) {
        JSONObject sendDataJson = new JSONObject();

        sendDataJson.put("d", JSON.parseArray(leakagePo.getData()));
        sendDataJson.put("m", leakagePo.getM());
        sendDataJson.put("time", leakagePo.getTime());
        sendDataJson.put("from", 1);

        //

        RequestBody requestBody = RequestBody.create(MediaType.parse(httpHeaderMediaType), sendDataJson.toJSONString());
        Request request = new Request.Builder()
                .addHeader(httpHeaderContentType, httpHeaderMediaType)
                .addHeader("Accept", httpHeaderMediaType)
                .post(requestBody)
                .url("http://localhost:8899/data1")
                .build();
        Response signRsp = null;
        try {
            signRsp = httpClient.newCall(request).execute();
            if (signRsp.isSuccessful()) {
            }
        } catch (IOException e) {
            log.error("连接服务 失败", e);
        }

    }

}
